package dao

import (
	"go-weixin/app/usercenter/model"
	"go-weixin/common/chendb"
	"go-weixin/common/utils"
)

type UserDao struct {
	engine chendb.ChenDb
	ut     utils.Utils
}

func NewUserDao(engine chendb.ChenDb) *UserDao {
	return &UserDao{
		engine: engine,
		ut:     *utils.UT,
	}
}

func (u UserDao) Register(name string, password string, user *model.UserModel) (int64, error) {
	user.Name = name
	user.Password = u.ut.Md5Encryption(password)
	return u.engine.Insert(user)
}

func (u UserDao) Login(name string, password string, user *model.UserModel) (bool, error) {
	user.Name = name
	user.Password = u.ut.Md5Encryption(password)
	return u.engine.Get(user)
}

func (u UserDao) UserExistByName(name string) (bool, error) {
	return u.engine.Exist(&model.UserModel{
		Name: name,
	})
}

func (u UserDao) GetUserByName(name string, user *model.UserModel) (bool, error) {
	return u.engine.Get(user)
}

func (u UserDao) UserUpdateExistByName(name string, id int64) (bool, error) {
	return u.engine.Where("name = ? AND id != ?", name, id).Exist(&model.UserModel{})
}

func (u UserDao) UserUpdateExistByEmail(email string, id int64) (bool, error) {
	return u.engine.Where("email = ? AND id != ?", email, id).Exist(&model.UserModel{})
}

func (u UserDao) UserUpdateExistByPhone(phone string, id int64) (bool, error) {
	return u.engine.Where("phone = ? AND id != ?", phone, id).Exist(&model.UserModel{})
}

func (u UserDao) UserUpdate(id int64, user *model.UserModel) (int64, error) {
	return u.engine.ID(id).Update(user)
}

func (u UserDao) UserUpdateRole(userId int64, oldRoleId int64, newRoleId int64) (int64, error) {
	return u.engine.Where("user_id = ? AND role_id = ?", userId, oldRoleId).Update(&model.UserRoleModel{RoleId: newRoleId})
}

func (u UserDao) UserAddRoleExist(userId int64, roleId int64) (bool, error) {
	return u.engine.Exist(&model.UserRoleModel{
		UserId: userId,
		RoleId: roleId,
	})
}
func (u UserDao) UserAddRole(userId int64, roleId int64) (int64, error) {
	return u.engine.Insert(&model.UserRoleModel{
		UserId: userId,
		RoleId: roleId,
	})
}

func (u UserDao) UserUpdateRoleExist(userId int64, newRoleId int64) (bool, error) {
	return u.engine.Exist(&model.UserRoleModel{UserId: userId, RoleId: newRoleId})
}

func (u UserDao) UserList(limit int64, offset int64, userList *[]model.UserModel) error {
	return u.engine.Limit(int(limit), int(offset)).Find(userList)
}

func (u UserDao) UserTest(limit int64, offset int64, userList *[]model.UserModel) error {
	return u.engine.Limit(int(limit), int(offset)).Find(userList)
}

func (u UserDao) GetUserById(id int64, user *model.UserModel) (bool, error) {
	return u.engine.ID(id).Get(user)
}

func (u UserDao) UserGetRole(id int64, roleList *[]model.UserGetRoleGroup) error {
	ur := new(model.UserRoleModel)
	r := new(model.RoleModel)
	return u.engine.Table(ur.TableName()).Alias("ur").
		Join("INNER", []string{r.TableName(), "r"}, "ur.role_id = r.id").
		Where("ur.user_id = ?", id).
		Find(roleList)
}

func (u UserDao) UserGetPower(id int64, powerList *[]model.PowerModel) error {
	ur := new(model.UserRoleModel)
	r := new(model.RoleModel)
	rp := new(model.RolePowerModel)
	p := new(model.PowerModel)
	return u.engine.Table(ur.TableName()).Alias("ur").
		Join("INNER", []string{r.TableName(), "r"}, "ur.role_id = r.id").
		Join("INNER", []string{rp.TableName(), "rp"}, "rp.role_id = r.id").
		Join("INNER", []string{p.TableName(), "p"}, "rp.power_id = p.id").
		Where("ur.user_id = ?", id).
		Select("p.*").
		Find(powerList)
}
